unit Actions;

interface

uses
  Database, AM, Functions, VarGrabber, refactorings,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, Grids, ExtCtrls, DB,
  DBTables, Mask, DBCtrls, ComCtrls, Menus, DBGrids, PortalGrid;

type
  TFormActions = class(TForm)
    Label5: TLabel;
    Label6: TLabel;
    BtnClear: TBitBtn;
    MainMenu1: TMainMenu;
    MenuHelp: TMenuItem;
    ba: TBitBtn;
    bb: TBitBtn;
    bc: TBitBtn;
    bd: TBitBtn;
    be: TBitBtn;
    bf: TBitBtn;
    bg: TBitBtn;
    bh: TBitBtn;
    bi: TBitBtn;
    bj: TBitBtn;
    bk: TBitBtn;
    bl: TBitBtn;
    bm: TBitBtn;
    bn: TBitBtn;
    bo: TBitBtn;
    bp: TBitBtn;
    bq: TBitBtn;
    br: TBitBtn;
    bs: TBitBtn;
    bt: TBitBtn;
    bu: TBitBtn;
    bv: TBitBtn;
    bw: TBitBtn;
    bx: TBitBtn;
    by: TBitBtn;
    bz: TBitBtn;
    Bevel4: TBevel;
    PanelMain: TPanel;
    LabelCommand: TLabel;
    BtnSet: TBitBtn;
    BtnDone: TBitBtn;
    EditNewCommand: TEdit;
    BtnHelp: TBitBtn;
    EditCommand: TEdit;
    ImagePointer: TImage;
    TimerPointer: TTimer;
    Invisible1: TMenuItem;
    CtrlA1: TMenuItem;
    Panel1: TPanel;
    Status: TLabel;
    BtnAM: TBitBtn;
    BtnFunctions: TBitBtn;
    BtnVar: TBitBtn;
    BtnClearCommand: TBitBtn;
    EditCopy: TEdit;
    CtrlF1: TMenuItem;
    CtrlR1: TMenuItem;
    DSChars: TDataSource;
    GridCharacter: TPortalGrid;
    procedure BtnDoneClick(Sender: TObject);
    procedure BtnClearClick(Sender: TObject);
    procedure BtnSetClick(Sender: TObject);
    procedure GridCharacterDblClick(Sender: TObject);
    procedure GridCharacterKeyPress(Sender: TObject; var Key: Char);
    procedure DSCharsDataChange(Sender: TObject; Field: TField);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure BtnHelpClick(Sender: TObject);
    procedure EditNewCommandKeyPress(Sender: TObject; var Key: Char);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure baClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TimerPointerTimer(Sender: TObject);
    procedure GridCharacterCalcCellColors(Sender: TObject; Field: TField;
      State: TGridDrawState; highlight: Boolean; AFont: TFont;
      ABrush: TBrush);
    procedure bqMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure BtnAMClick(Sender: TObject);
    procedure BtnFunctionsClick(Sender: TObject);
    procedure BtnVarClick(Sender: TObject);
    procedure BtnClearCommandClick(Sender: TObject);
  private
    { Private declarations }
    procedure UpdateButtons;
    procedure UpdatePointer;
  public
    { Public declarations }
  end;

var
  FormActions: TFormActions;

implementation

{$R *.DFM}

const
   TABLE_ACTIONS_FILTER = '(Master = %s) and (Character = ''%s'')';

procedure TFormActions.FormCreate(Sender: TObject);
begin
  with Datawell.TableChars do begin
    filter := '';
    filtered := true;
    active:=true;
    first;
  end;
end;

procedure TFormActions.FormShow(Sender: TObject);
begin
  UpdateButtons;
end;

procedure TFormActions.BtnDoneClick(Sender: TObject);
begin
  close;
end;

procedure TFormActions.BtnClearClick(Sender: TObject);
begin
  DSCharsDataChange(sender,nil);
  if (EditCommand.text <> '') and (not Datawell.TableActions.EOF) then
    Datawell.TableActions.delete;
  EditCommand.text:='';
  EditNewCommand.setfocus;
  UpdateButtons;
end;

procedure TFormActions.BtnSetClick(Sender: TObject);
begin
  DSCharsDataChange(sender,nil);
  if EditNewCommand.text = '' then begin
    messagedlg('You must enter a command',mtinformation,[mbok],0);
    EditNewCommand.setfocus;
    exit;
  end;
  with Datawell.TableActions do begin
    if EditCommand.text <> '' then begin
      edit;
      Datawell.TableActions.fieldbyname('Action').asstring:=EditNewCommand.text;
      post;
    end
    else begin
      append;
      Datawell.TableActions.fieldbyname('Master').asinteger:=Datawell.masterinteger;
      Datawell.TableActions.fieldbyname('Character').asstring:=Datawell.TableChars.fieldbyname('Character').asstring;
      Datawell.TableActions.fieldbyname('Action').asstring:=EditNewCommand.text;
      post;
    end;
  end;
  EditNewCommand.text:='';
  UpdateButtons;
  DSCharsDataChange(sender,nil);
end;

procedure TFormActions.GridCharacterDblClick(Sender: TObject);
begin
  EditNewCommand.text:=EditCommand.text;
  EditNewCommand.selectall;
  EditNewCommand.setfocus;
end;

procedure TFormActions.GridCharacterKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    GridCharacterDblClick(sender);
  end;
end;

procedure TFormActions.DSCharsDataChange(Sender: TObject;
  Field: TField);
begin
  with Datawell.TableActions do begin
    filter := format(TABLE_ACTIONS_FILTER,
                     [Datawell.masterindex, Datawell.TableChars.fieldbyname('Character').asstring]);
    filtered := true;
    active:=true;
  end;
  EditCommand.text:=Datawell.TableActions.fieldbyname('Action').asstring;
  UpdatePointer;
end;

procedure TFormActions.FormMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
  Status.caption:=' '+(sender as TControl).hint;
end;

procedure TFormActions.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('quickchars');
end;

procedure TFormActions.EditNewCommandKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnSet.click;
  end;
end;

procedure TFormActions.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key = VK_DELETE) and (activecontrol = GridCharacter) then begin
    BtnClear.click;
    key:=0;
  end;
end;

procedure TFormActions.UpdateButtons;
var
  i: integer;
  hin: string;
begin
  for i:=0 to componentcount-1 do begin
    if TComponent(components[i]).tag <> 1 then continue;
    hin:=TBitBtn(components[i]).hint;
    with Datawell.TableActions do begin
      filter := format(TABLE_ACTIONS_FILTER, [Datawell.masterindex, hin]);
      filtered := true;;
      active:=true;
    end;
    if Datawell.TableActions.fieldbyname('Action').asstring <> '' then begin
      TBitBtn(components[i]).font.style:=[fsbold];
      TBitBtn(components[i]).font.color:=clwhite;
    end
    else begin
      TBitBtn(components[i]).font.style:=[];
      TBitBtn(components[i]).font.color:=clblack;
    end;
  end;
end;

procedure TFormActions.baClick(Sender: TObject);
begin
  with Datawell.TableChars do begin
    first;
    while (not EOF) and
      (TBitBtn(sender).hint <> Datawell.TableChars.fieldbyname('Character').asstring) do next;
  end;
  EditNewCommand.setfocus;
  TimerPointer.enabled:=true;
end;

procedure TFormActions.UpdatePointer;
begin
  DrawArrow(GridCharacter, ImagePointer, 300, -8, 10);
end;

procedure TFormActions.TimerPointerTimer(Sender: TObject);
begin
  UpdatePointer;
  TimerPointer.enabled:=false;
end;

procedure TFormActions.GridCharacterCalcCellColors(Sender: TObject;
  Field: TField; State: TGridDrawState; highlight: Boolean; AFont: TFont;
  ABrush: TBrush);
begin
  with Datawell.TableActions do begin
    filter := format(TABLE_ACTIONS_FILTER, [Datawell.masterindex, string(field.Value)]);
    filtered := true;
    active:=true;
  end;
  if Datawell.TableActions.fieldbyname('Action').asstring <> '' then begin
    afont.color:=clwhite;
    if highlight then abrush.color:=clgray else abrush.color:=clsilver;
  end;
end;

procedure TFormActions.bqMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Status.caption:='';
end;

procedure TFormActions.BtnAMClick(Sender: TObject);
var
  form: TFormAM;
  am: string;
begin
  form:=TFormAM.create(self);
  form.setup;
  form.showmodal;
  am:=form.am;
  form.free;
  if am = '' then exit;
  EditNewCommand.text:=am;
  EditNewCommand.setfocus;
end;

procedure TFormActions.BtnFunctionsClick(Sender: TObject);
var
  form: TFormFunctions;
  i, po: integer;
begin
  form:=TFormFunctions.create(self);
  form.Setup(Datawell.lastfunction,EditNewCommand.seltext);
  form.showmodal;
  po:=form.po;
  form.free;
  i:=EditNewCommand.selstart;
  if po < 1 then begin
    EditNewCommand.setfocus;
    exit;
  end;
  EditNewCommand.pastefromclipboard;
  EditNewCommand.setfocus;
  EditNewCommand.sellength:=0;
  EditNewCommand.selstart:=i+po;
end;

procedure TFormActions.BtnVarClick(Sender: TObject);
var
  form: TFormVarGrab;
  grab: string;
  i, l: integer;
begin
  form:=TFormVarGrab.create(self);
  form.setup;
  form.showmodal;
  grab:=form.grab;
  form.free;
  i:=EditNewCommand.selstart;
  l:=EditNewCommand.sellength;
  if grab = '' then begin
    EditNewCommand.setfocus;
    EditNewCommand.selstart:=i;
    EditNewCommand.sellength:=l;
    exit;
  end;
  EditCopy.text:=grab;
  EditCopy.selectall;
  EditCopy.copytoclipboard;
  EditNewCommand.pastefromclipboard;
  EditNewCommand.setfocus;
  EditNewCommand.sellength:=0;
  EditNewCommand.selstart:=i+length(EditCopy.text);
end;

procedure TFormActions.BtnClearCommandClick(Sender: TObject);
begin
  EditNewCommand.text:='';
  EditNewCommand.setfocus;
end;

end.                 
